﻿using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SimulationCenter.Schedulers;

namespace SimulationCenter
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            Task.Factory.StartNew(async () =>
            {
                var logger = host.Services.GetRequiredService<ILogger<Program>>();

                logger.LogInformation("adb server starting...");

                var adbCenter = host.Services.GetRequiredService<AdbCenter>();
                await adbCenter.EnsureServerRunning();

                logger.LogInformation("adb server started.");

                logger.LogInformation("recovery tasks");

                var scheduleManager = host.Services.GetRequiredService<ScScheduleManager>();
                await scheduleManager.RecoveryTasksSchedule();

                logger.LogInformation("recovery tasks running...");
            });

            host.Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}
